package com.wandian.message.mvvm.sysmessage;

import android.os.Bundle;
import android.view.View;

import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.mylib.libcore.AppContext;
import com.mylib.libcore.config.RouterConfig;
import com.mylib.libcore.mvvm.BaseMVVMActivity;
import com.mylib.libcore.utils.JsonUtil;
import com.mylib.libcore.utils.StatusBarUtils;
import com.mylib.libcore.utils.UIUtils;
import com.wandian.message.R;
import com.wandian.message.adapter.MessageRecycleAdapter;
import com.wandian.message.bean.SysMessageBean;
import com.wandian.message.databinding.MessageActivitySysMessageBinding;

/**
 * @author Administrator
 * @date 2019/12/18-14:28
 * @description 消息
 **/
@Route(path = RouterConfig.ROUTER_ACTIVITY_SYS_MESSAGE)
public class MessageActivity extends BaseMVVMActivity<MessageActivityViewModel> implements  BaseQuickAdapter.RequestLoadMoreListener {
    private MessageActivitySysMessageBinding dataBinding;
    private MessageRecycleAdapter messageRecycleAdapter;
    private int pageNum =1;
    private int pageSize = 30;
    private boolean isReflash = true;
    @Override
    public void init(Bundle savedInstanceState) {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.message_activity_sys_message);
        dataBinding.setLifecycleOwner(this);
        dataBinding.setViewModel(mViewModel);
        initViewData();
        initClink();
        initRecycleView();
    }

    @Override
    protected void onResume() {
        super.onResume();
        getMessageData();
    }
    private void getMessageData( ){
        if(isReflash){
            pageNum = 1;
        }else {
            pageNum =pageNum+ 1;
        }
        mViewModel.appMessageList(pageNum,pageSize);
    }

    private void initViewData(){
        StatusBarUtils.with(this,false)
                .setIsActionBar(false)
                .clearActionBarShadow()
                .setDrawable(getResources().getDrawable(R.drawable.sys_color_two))
                .init();
        dataBinding.bindSysToolBar.toolBarTvTitle.setText("消息");
        mViewModel.messageListDataBean.observe(this,messageListDataBean -> {

            dataBinding.srlRefresh.setRefreshing(false);
            if(!UIUtils.isEmpty(messageListDataBean)){
                if(messageListDataBean.size() != pageSize) messageRecycleAdapter.setEnableLoadMore(false);
                if(isReflash){
                    messageRecycleAdapter.setNewData(messageListDataBean);
                }else {
                    messageRecycleAdapter.addData(messageListDataBean);
                    messageRecycleAdapter.loadMoreComplete();
                }
            }else {
                if(isReflash){
                    messageRecycleAdapter.setEmptyView(R.layout.empty_view,null);
                }
                messageRecycleAdapter.setEnableLoadMore(false);
            }

        });
    }
    private void initClink(){
        dataBinding.bindSysToolBar.toolBarIvBack.setOnClickListener(v -> {
            finishhideSoftBoard();
            finish();
        });

    }

    private void initRecycleView(){
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
        dataBinding.recycler.setLayoutManager(layoutManager);
        messageRecycleAdapter  = new MessageRecycleAdapter(R.layout.message_item_message_style_one);
        dataBinding.recycler.setAdapter(messageRecycleAdapter);
        messageRecycleAdapter.setEmptyView(getLayoutInflater().inflate(R.layout.empty_view, null));
        messageRecycleAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
                SysMessageBean bean = (SysMessageBean)messageRecycleAdapter.getData().get(i);
                if(!UIUtils.isEmpty(bean)){
                    AppContext.getInstance().getAppPreferencesHelper().setKeyValue("read_message_ids",bean.getId()+"=");
                    ARouter.getInstance().build(RouterConfig.ROUTER_ACTIVITY_SYS_MESSAGE_DETAILS).withString("message_json", JsonUtil.toJson(bean)).navigation();
                }
            }
        });

        dataBinding.srlRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                isReflash = true;
                getMessageData();
            }
        });
        messageRecycleAdapter.setOnLoadMoreListener(this, dataBinding.recycler);

    }

    @Override
    public void onLoadMoreRequested() {
        if(!UIUtils.isEmpty(messageRecycleAdapter) && messageRecycleAdapter.getData().size()>=pageSize){
            isReflash = false;
            getMessageData();
        }else {
            messageRecycleAdapter.setEnableLoadMore(false);
        }

    }

}
